
// The puropse of this function is to plot the Y co-ordinate against x co-ordinate (For Ex., Y vs X) of the given single curve
function abaqus_SingleCurve(curve) {
	var X = 0;
	var Y = 1;
	var s = "";

	for ( var i = 0; i < curve.length; i++) {
		s += formatRealToExponential(curve[i][Y]) + "," + "\t" + formatRealToExponential(curve[i][X]) + "\n";
	}
	s+="**" + "\n";
	return s;
}

function abaqus_MultipleCurves(curves) {
	// curve is a nested array [series][X = 0, Y = 1]
	//This function expects all the curves to be at common X points
	var X = 0;
	var Y = 1; 
	var s = "";
	
	if(curves == null)
		return s;
	
	if(!isAllCurvesAtCommonXPoints(curves)) {
		throw new javax.script.ScriptException("Error : abaqus_MultipleCurves function require all curves at common X-Axis points.")
	}
	  
	var iNum = 0;
	var iRow = getNumPointsForMultiCurves(curves);
	if(iRow == 0)
		return s;
	var iTotalCurves = curves.length;
	
	// Write curves data
	for(var iNumRow = 0 ; iNumRow < iRow ; iNumRow++) {
		//s += "\t";
		var commonX = 0;
		for(var i = 0 ; i < iTotalCurves ; i++)  {			
				var curve = curves[i];
				if(curve != null) {
					s += formatRealToExponential(curve[iNumRow][Y]) + "," + "\t";
					commonX = curve[iNumRow][X];
				}else
					s += formatRealToExponential(0) + "," + "\t";
					
		}
		s += formatRealToExponential(commonX) + "\n";
	}
	s+="**" + "\n";
	return s;
}

function abaqus_IsoTempElasticCard(Curves)
{ 
	var s = "*ELASTIC, TYPE=ISOTROPIC "+"\n";
	var X = 0;
    var Y = 1;
	var ss = ", ";
	var ssn = "\n";
	  
	var iNum = 0;
	var iRow = getNumPointsForMultiCurves(Curves);
	var iTotalCurves = Curves.length;
	
	// Write E,NU,Temperature
	for ( var iNumRow = 0; iNumRow < iRow; iNumRow++) {
		for ( var i = 0; i < iTotalCurves-1 ; i++) {
			var curve = Curves[i];
			s += formatRealToExponential(curve[iNumRow][Y]) + ", ";
		}
		s += formatRealToExponential(curve[iNumRow][X]) + "\n";
	}

	// Write density data.
	var iLastCurve = Curves[iTotalCurves - 1];
	s += "*Density" + "\n";

	if (iLastCurve != null) {
		for ( var i = 0; i < iLastCurve.length; i++) {
			s += formatRealToExponential(iLastCurve[i][Y]) + ", " + formatRealToExponential(iLastCurve[i][X]) + "\n";
		}
	}
	
	return s;
}

// 	This function plots multiple Y co-ordinates against common X co-ordinate (For Ex., Y1 vs Y2 vs... vs X) for common data points 
//	for given multiple curves of Isotropic dynamic card.
function abaqus_IsotropicElasticCard(Curves)
{ 
	var s = "*ELASTIC, TYPE=ISOTROPIC "+"\n";
		s += abaqus_MultipleCurves(Curves);
	return s;
}

function abaqus_OrthoTempElasticCard(curves,densityCurve, iPrefix) {
	var s = abaqus_OrthoDynamicCard(curves,iPrefix);
	var X = 0;
	var Y = 1;
	// Write density data.
	if (densityCurve != null) {
		s += "*Density" + "\n";
		for ( var i = 0; i < densityCurve.length; i++) {
			s += formatRealToExponential(densityCurve[i][Y]) + ", " + formatRealToExponential(densityCurve[i][X]) + "\n";
		}
	}

	return s;
}

//This function plots multiple Y co-ordinates against common X co-ordinate (For Ex., Y1 vs Y2 vs... vs X) for common data points 
//for given multiple curves of Orthotropic dynamic card.
function abaqus_OrthoDynamicCard(curves,iPrefix) {
	var s = "*ELASTIC,";
	var X = 0;
	var Y = 1;
	// curve is a nested array [series][X = 0, Y = 1]

	if(iPrefix == 0)//For Orthotropic Solid
		s += "TYPE=ENGINEERING CONSTANTS";
	else if(iPrefix == 1)//For Orthotropic Shell
		s += "TYPE=LAMINA";
	s +="\n";

	var iNum = 0;
	var iRow = getNumPointsForMultiCurves(curves);
	var iTotalCurves = curves.length;
	if(iTotalCurves <= 8) {
		for ( var iNumRow = 0; iNumRow < iRow; iNumRow++) {
			var commonX = 0;
			for ( var i = 0; i < iTotalCurves ; i++) {
				var curve = curves[i];
				if(curve != null){
					s += formatRealToExponential(curve[iNumRow][Y]) + ",";
					commonX = curve[iNumRow][X];
				} else {
					s +=  formatRealToExponential(0) +",";
				}
				
				if(i == iTotalCurves - 1)
					s += formatRealToExponential(commonX) + "\n";
			}
		}
	} else {
		for ( var iNumRow = 0; iNumRow < iRow; iNumRow++) {
			var commonX = 0;
			for ( var i = 0; i < iTotalCurves-2 ; i++) {
				var curve = curves[i];
				if(curve != null){
					s += formatRealToExponential(curve[iNumRow][Y]) + ",";
					commonX = curve[iNumRow][X];
				} else {
					s +=  formatRealToExponential(0) +",";
				}
			}
			var iSecondLastCurve = curves[iTotalCurves - 2];
			if(iSecondLastCurve != null) {
				s+= formatRealToExponential(iSecondLastCurve[iNumRow][Y]) + "\n";
				commonX = curve[iNumRow][X];
			} else
				s += formatRealToExponential(0) +"\n";
			
			var iLastCurve = curves[iTotalCurves - 1];
			if(iLastCurve != null) {
				s += formatRealToExponential(iLastCurve[iNumRow][Y]) + ",";
				commonX = curve[iNumRow][X];
			}else
				s += formatRealToExponential(0) +",";
			
			s += formatRealToExponential(commonX) + "\n";
		}
	}

	// Write E,NU,G,Temperature
		
	s+="**" + "\n";
	return s;
}

function abaqus_FailStressCard(STvsTempCAE, ST, SCvsTempCAE, SC, SSvsTempCAE, SS) {
	var X = 0;
	var Y = 1;
	var s = "";
	var TREF = 0.0;
	if (ST != 0 && SC != 0 && SS != 0 && STvsTempCAE == null && SCvsTempCAE == null && SSvsTempCAE == null) {
		s += formatRealToExponential(ST) + ", " + formatRealToExponential(SC) + ", " + formatRealToExponential(ST) + ", " + formatRealToExponential(SC) + ", " + formatRealToExponential(SS) + ", 0.0, 0.0, " + formatRealToExponential(TREF) + "\n";
	} else if (STvsTempCAE != null && SCvsTempCAE != null && SSvsTempCAE != null) {
		for (var i = 0; i < STvsTempCAE.length; i++) {
			s += formatRealToExponential(STvsTempCAE[i][Y]) + ", " + formatRealToExponential(SCvsTempCAE[i][Y]) + ", " + formatRealToExponential(STvsTempCAE[i][Y]) + ", " + formatRealToExponential(SCvsTempCAE[i][Y]) + ", " + formatRealToExponential(SSvsTempCAE[i][Y]) + ", 0.0, 0.0, " + formatRealToExponential(STvsTempCAE[i][X]) + "\n";
		}
	}

	return s;
}

function abaqus_PlasticFamilyOfCurvesAtTemperature(curveFamily, iMID, tempParamName, curveInputName) {
	/*
	 * *Plastic
712.45,             0,	 20.
725.1,	     7.77E-05,	 20.
737.76,	      0.00012,	 20.
750.42,	     0.000183,	 20.
763.08,	     0.000278,	 20.
775.74,	      0.00042,	 20.
788.39,	     0.000629,	 20.
801.05,	     0.000937,	 20.
	 */
	var X = 0;
	var Y = 1;
	var s = "*Plastic\n";
	var comma = ", ";
	var familyParameterGroups = curveFamily.getFamilyParameterGroups();
	var numParameterGroups = familyParameterGroups.size();
	
	for(var i = 0; i < numParameterGroups; i++) {
		var paramSet = familyParameterGroups.get(i);
		var parameter =  paramSet.getParameterByName(tempParamName);
		var paramValue = 0;
		if(parameter != null) {
			paramValue = parameter.getValue();
			if(paramValue == null)
				paramValue = 0;
		}		
		
		var curveInput = paramSet.getInputByName(curveInputName);
		var curve = curveInput.getValue();
		for(var j = 0; j < curve.length; j++) {
			s += String.format("%s, %s, %s\n", formatRealToExponential(curve[j][Y]), formatRealToExponential(curve[j][X]), formatRealToExponential(paramValue));
		}
	}
	return s;
}

function abaqus_HillCoefficientsAtTemperature(curveFamily, iMID, tempParamName) {
	/*
	 *Potential
1.,     1.,     1., 1.2837, 1.2837, 1.2837,    20.
1.,     1.,     1., 1.2343, 1.2343, 1.2343,   450.
	 */
	return abaqus_AllCoefficientsAtTemperature(curveFamily, iMID, tempParamName, "Potential");
}

function abaqus_StrengthRatiosAtTemperature(curveFamily, iMID, tempParamName) {
	/*
	 *Potential
1.,     1.,     1., 1.2837, 1.2837, 1.2837,    20.
1.,     1.,     1., 1.2343, 1.2343, 1.2343,   450.
	 */
	return abaqus_AllCoefficientsAtTemperature(curveFamily, iMID, tempParamName, "Potential");
}

function abaqus_AllCoefficientsAtTemperature(curveFamily, iMID, tempParamName, cardName) {
	var X = 0;
	var Y = 1;
	var s = "*" + cardName + "\n";
	var comma = ", ";
	var familyParameterGroups = curveFamily.getFamilyParameterGroups();
	var numParameterGroups = familyParameterGroups.size();
	
	for(var i = 0; i < numParameterGroups; i++) {
		var paramSet = familyParameterGroups.get(i);
		var parameter =  paramSet.getParameterByName(tempParamName);
		var paramValue = 0;
		if(parameter != null) {
			paramValue = parameter.getValue();
			if(paramValue == null)
				paramValue = 0;
		}		
		var inputs = paramSet.getFamilyInputs();
		var numInputs = inputs.size();
		for(var j = 0; j < numInputs; j++) {
			var hillCoefficientInput = inputs.get(j);
			var coeffValue = hillCoefficientInput.getValue();
			s += formatRealToExponential(coeffValue) + comma;
		}
		
		s+= formatRealToExponential(paramValue) + "\n";
	}
	return s;
}